<script type="text/javascript">
$(function(){
	$.get("applicationSingle/getCardInfo",function(data){
		var content="<option value='' selected>请选择</option>";
		var i;
		$(data).each(function(n,item){
			for(i=0;i<item.certificates.length;i++){
				content+="<option value="+item.certificates[i].certificateType+">"
				+item.certificates[i].certificatesName+"</option>"
			}
			$("#agentPapertypeByGetCard").html(content);
		});
	},"json");
	
	$.post("issueCardSingle/getCardRegisterInfo",function(json){
		if(json.statusCode==DWZ.statusCode.error){
			navTabAjaxDone(json)
		}else{
			$(json).each(function(n,item){
				$("input[name='customerName']",navTab.getCurrentPanel()).val(item.scv.customerName);
				$("input[name='cardPCode']",navTab.getCurrentPanel()).val(item.scv.cardPCode);
				$("input[name='paperTypeName']",navTab.getCurrentPanel()).val(item.scv.paperTypeName);
				$("input[name='paperType']",navTab.getCurrentPanel()).val(item.scv.paperType);
				$("input[name='paperNo']",navTab.getCurrentPanel()).val(item.scv.paperNo);
				$("input[name='cardPCodeName2']",navTab.getCurrentPanel()).val(item.scv.cardPCodeName);
				$("input[name='applyType']",navTab.getCurrentPanel()).val(item.scv.applyType);
				$("input[name='cardCost']",navTab.getCurrentPanel()).val((item.fee).toFixed(2))
				if(item.fee==0){
					alertMsg.warn("工本费未定义或者有效期过期")
				}
				var contenta="<label>原卡应用：</label>";
				var contentb="<label>新卡开通应用：</label>"
				if(item.scv.applyType=="1"){//首次申请时将原卡信息隐藏
					for(var i=0;i<item.apps.length;i++){
						if(item.apps[i].appFlag=="3F01")
						contentb+="<input name='appCodes' type='checkbox' value="+item.apps[i].appFlag+" checked='checked'/>"+item.apps[i].appName+"&nbsp;&nbsp;"				
					}
					$("#oldCardByGetCard").attr("hidden","hidden");
					$("#newCardApp").html(contentb);
				
				}else{//补换卡时
					$("input[name='oldCardPCodeName']",navTab.getCurrentPanel()).val(item.scv.cardPCodeName);
					$("input[name='oldCitizenCardNo']",navTab.getCurrentPanel()).val(item.scv.oldCitizenCardNo);
					$("input[name='oldCardTypeName']",navTab.getCurrentPanel()).val(item.scv.cardTypeName);
					$("input[name='oldCitizenBalance']",navTab.getCurrentPanel()).val(item.scv.oldCitizenBalance);
					$("input[name='blackFlagName']",navTab.getCurrentPanel()).val(item.scv.blackFlagName);
					$("input[name='blackFlagReason']",navTab.getCurrentPanel()).val(item.scv.blackFlagReason);
					$("input[name='blackList']").val(item.scv.applyTypeName);
					var list=item.scv.appCodes;
					for(var i=0;i<list.length;i++){
						for(var i=0;i<item.apps.length;i++){
							//alert(list[i]+","+item.apps[i].appFlag)
							if(list[i]==item.apps[i].appFlag){
								contenta+="<input name='oldApp' type='checkbox' checked='checked' value="+item.apps[i].appFlag+" readonly='readonly'/>"+item.apps[i].appName+"&nbsp;&nbsp;"
								contentb+="<input name='appCodes' type='checkbox'  checked='checked' value="+item.apps[i].appFlag+" readonly='readonly'/>"+item.apps[i].appName+"&nbsp;&nbsp;"
								break;
							}				
						}
					}
					$("#oldCardApp").html(contenta);
					$("#newCardApp").html(contentb);
				}
				
			})
		}
		
	},"json"); 

});
function readCardBySingleCard(){
	var flag=activeCPU();
	if(flag){
		flag=qzsmkocx.GetSmkNo();//获取市民卡号
	}
	var citizenCardNo=""
	if(flag){
		citizenCardNo=qzsmkocx.GetRetValue().split(",")[0];
		$("input[name='newCitizenCardNo']").val(citizenCardNo);
		//通过市民卡号获取数据
		$.post("sellCard/queryCardData",{citizenCardNo:citizenCardNo},function(json){
			if(json.statusCode==DWZ.statusCode.ok){
				$(json).each(function(n,item){
					$("input[name='cardTypeName']").val(item.cardData.cardTypeName);
					$("input[name='cardPCodeName']").val(item.cardData.cardPName);
					$("input[name='getCardTime']").val(getDate(Date.now()));
					$("input[name='cardPCode']").val(item.cardData.cardPCode);
				})
			}else{
				alertMsg.warn(json.message);
			}
			
		},"json")	
	}else{
		checkError(getError());
	}
}

function writeInfoTocard(){
	var sdata="";
	var cardPCodeName2=$("input[name='cardPCodeName2']").val();
	var cardPCodeName=$("input[name='cardPCodeName']").val();
	if(cardPCodeName!=cardPCodeName2){
		alertMsg.warn("卡产品不一致");
		return false;
	}
	var operatorNo="";
	//卡主类型16进制
	var key_cardTypeA=cardPcodeToHex($("input[name='cardPCode']").val());
	$.ajax({
		type: 'POST',
		url:"getOperator",
		dataType:"json",
		async: false,
		success: function(json){
			operatorNo=json.userNo;
		},
	})
	var idate=writeDate(Date.now())
	sdata="key_cardTypeA,"+key_cardTypeA+"|key_idate,"+idate+"|key_operator,"+operatorNo;
	//alert(sdata)
	var blnRs=updateInfo_3F00(sdata)//写3F00
	if(!blnRs){
		checkError(getError());
		return false;
	}
	var customerNameGBK=writeName($URL.encode($("input[name='customerName']").val()));
	var paperNo=writePaperNo($("input[name='paperNo']").val());
	var paperType=$("input[name='paperType']").val();
	//选中了几个应用
	$("input[name='appCodes']:checked").each(function(n,item){
		var adf=$(this).val();
		//alert(adf)
		var updateInfoA,updateInfoB;
		if(adf=="3F01"){
			updateInfoA="key_appid,02|key_sdate,"+idate+"|key_edate,"+afterDate(2)+"|key_cardtype,"+key_cardTypeA;
			updateInfoB="key_name,"+customerNameGBK+"|key_certificationNo,"+paperNo+"|key_certificationType,"+paperType
			blnRs=updateBaseInfo(adf,updateInfoA);//0015
			if(!blnRs){
				checkError(getError());
				return false;
			}
			blnRs=updatePersonInfo(adf,updateInfoB);//016
			if(!blnRs){
				checkError(getError());
				return false;
			}
		}
		if(adf=="3F02"){
			updateInfoA="key_appid,02|key_sdate,"+idate;
			blnRs=updateBaseInfo(adf,updateInfoA);//0015
			if(!blnRs){
				checkError(getError());
				return false;
			}
		}
		if(adf=="3F10"){
			updateInfoB="key_name,"+customerNameGBK+"|key_certificationNo,"+paperNo+"|key_certificationType,"+paperType
			blnRs=updatePersonInfo(adf,updateInfoB);//0016
			if(!blnRs){
				checkError(getError());
				return false;
			}
		}
		if(adf=="9A00"){
			updateInfoA="key_appid,02|key_sdate,"+idate;
			blnRs=updateBaseInfo(adf,updateInfoA);//0015
			if(!blnRs){
				checkError(getError());
				return false;
			}
			//9A00 16文件暂时不更新
		}
		if(adf=="M1BK"){
			var f=findLogicCard();
			if(f){
				var flag=true;
				//读取0号扇区0块信息
				var info=readBlockExternal(initKey,0,0,0);
				var newCardsn=info.substr(0,8);
				var oldCitizenCardNo=$("input[name='oldCitizenCardNo']").val();
				var newCitizenCardNo=$("input[name='newCitizenCardNo']").val();
				var mode=newCitizenCardNo.substr(0, 2)=="11"?modeA:modeA
				var param={oldCitizenCardNo:oldCitizenCardNo,newCitizenCardNo:newCitizenCardNo,newCardsn:newCardsn}
				$.ajax({
					type: 'POST',
					url:"issueCardSingle/getExchangeNewCardInfo",
					dataType:"json",
					data:param,
					async: false,
					success:function(json){
						if(json.statusCode==DWZ.DWZ.statusCode.ok){
							var keys=json.key;
							var data=json.data;
							if(keys.length>0&&data.length>0){
								for(var i=0;i<keys.length;i++){
									//格式化扇区密钥(模式4)
									var sector=parseInt(keys[i].no)
									var writeInfo=keys[i].keya+keys[i].contrlword+keys[i].keyb
									var f= WriteBlockExternal(initKey,sector,sector*4+3,mode,writeInfo);
									if(!f){
										alertMsg.warn("自行车应用M1格式化扇区失败");
										checkError(getError());
										flag=false;
										return false
									}
								}
								for(var j=0;j<data.length;j++){
									var sector2=parseInt(data[j].no)
									for(var i=0;i<keys.length;i++){
										var sector=parseInt(keys[i].no)
										if(sector==sector2){
											//写扇区(模式0)
											var ff= writeSectorExternal(keys[i].keya,sector2,0,data[i].data);
											if(!ff){
												alertMsg.warn("自行车应用M1写扇区失败");
												checkError(getError());
												flag=false;
												return false;
											}
										}
									}		
								}
							}
							
						}else{
							navTabAjaxDone(json)
							flag=false;
						}					
					},
					error: DWZ.ajaxError
				})	
				//终止提交
				if(!flag){
					return false
				}
			}else{
				//寻卡失败
				checkError(getError());
				return false;
			}
		}
		
	})
	
	return true;
}
</script>

<div class="pageContent">
	<form id="commitGetCardRegister" method="post" action="issueCardSingle/commitGetCardRegister" class="required-validate" onsubmit="return validateCallback(this,printViewPageAjaxDone);">
		<div class="pageFormContent" layoutH="96">
			<fieldset>
			<legend>个人信息</legend>
			<p>
				<label>姓名：</label>
				<input  type="text" class="" size="30"name="customerName" readonly="readonly" />		
			</p>
			<p>
				<label>证件类型：</label>
				<input type="text"   name="paperTypeName" size="30" readonly="readonly"/>
				<input type="hidden"   name="paperType" size="30" readonly="readonly"/>
			</p>
			<p>
				<label>证件号码：</label>
				<input name="paperNo" class="" type="text" size="30" readonly="readonly" />
				<input name="cardPCodeName2" class="" type="hidden"  />
			</p>
			</fieldset>
			<div id="oldCardByGetCard">
			<fieldset>
			<legend>原卡信息</legend>
			<p>
				<label>卡产品：</label>
				<input  name="oldCardPCodeName" type="text" size="30" readonly="readonly"/>
			</p> 
			<p>
				<label>卡类型：</label>
				<input  name="oldCardTypeName" type="text" size="30" readonly="readonly"/>
			</p>
			<p>
				<label>原市民卡号：</label>
				<input name="oldCitizenCardNo"  type="text" size="30" readonly="readonly" />
			</p>
			  <p>
				<label>黑名单状态：</label>
				<input name="blackFlagName" type="text" size="30" readonly="readonly"/>
			</p>
			<p>
				<label>进入黑名单原因：</label>
				<input name="blackFlagReason" type="text" size="30"  readonly="readonly"/>
			</p>
			<!-- <p>
				<label>电子钱包余额(元)：</label>
				<input name="oldCitizenBalance" type="text" size="30"  readonly="readonly"/>
			</p> -->
			<p id="oldCardApp">
				 <label>原卡开通应用：</label>
			</p>
			</fieldset>
			</div>
			
			<fieldset>
			<legend>新卡信息</legend>
			
			<p>
				<label>新市民卡号：</label>
				<input name="newCitizenCardNo"  type="text" size="20" readonly="readonly" />
				&nbsp;&nbsp;<input type="button" value="读卡" onclick="readCardBySingleCard()">
			</p>
			<p>
				<label>卡产品：</label>
				<input  name="cardPCodeName" type="text" size="30" readonly="readonly"/>
				<input  name="cardPCode" type="hidden" size="30" />
			</p> 
			<p>
				<label>卡类型：</label>
				<input  name="cardTypeName" type="text" size="30" readonly="readonly"/>
			
			</p>
			  <p>
				<label>发卡日期：</label>
				<input name="getCardTime" type="text" size="30" readonly="readonly"/>
			</p>
			<p>
				<label>发卡收费(工本费)：</label>
				<input name="cardCost" type="text" size="30" class=""/>
			</p> 
			<!-- <p>
				<label>卡消费方：</label>
				<input name="getCardTime" type="text" size="30"  readonly="readonly"/>
			</p> -->
			<p id="newCardApp">
				<label>新卡开通应用：</label>
				
			</p>
			</fieldset>
			
			<fieldset>
			<legend>代理人信息</legend>
			<p>
				<label>代理人证件类型：</label>
				<select flag="paperCode" id="agentPapertypeByGetCard" name="agentPapertype" >
					
				</select>
			</p>
			<p>
				<label>代理人证件号码：</label>
				<input type="text" size="30" name="agentPaperNo" class="validIdentity"/>
			</p>
			<p>
				<label>代理人姓名：</label>
				<input type="text" size="30" name="agentName" class="validName"/>
			</p>
			<p>
				<label>代理人手机号码：</label>
				<input type="text" size="30" name="agentPhone" class="validMobile"/>
			</p>
			<p>
				<label>备注：</label>
				<input name="desc" type="text" size="30" maxlength="30" />
			</p>
			</fieldset>
			<p>
				<input name="applyType" type="hidden" />
			</p>
			
		</div>
		<div class="formBar">
			<ul>
				<li><div class="buttonActive"><div class="buttonContent"><button type="submit" class="valid" onclick="return writeInfoTocard();">提交</button></div></div></li>
				<li>
					<a class="buttonActive" href="issueCardSingle/getCardSingle" target="navTab" rel="exchangeCard" title="单笔发卡"><span>返回</span></a>
				</li>
			</ul>
		</div>
	</form>
</div>